package com.kanjj.mywebrtc.network;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.webrtc.DataChannel;
import org.webrtc.IceCandidate;
import org.webrtc.MediaStream;
import org.webrtc.MediaStreamTrack;
import org.webrtc.PeerConnection;
import org.webrtc.RtpReceiver;
import org.webrtc.RtpTransceiver;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoTrack;


class PeerConnectionObserver implements PeerConnection.Observer {


    private String TAG = "PeerConnectionObserver";
    private String socketId;
    private boolean initial;

    public PeerConnectionObserver(String socketId, boolean initial) {
        this.socketId = socketId;
        this.initial = initial;
    }


    @Override
    public void onSignalingChange(PeerConnection.SignalingState signalingState) {
        Log.i(TAG, "onSignalingChange: " + signalingState);
    }

    @Override
    public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) {
        Log.i(TAG, "onIceConnectionChange: " + iceConnectionState);
    }

    @Override
    public void onIceConnectionReceivingChange(boolean b) {
        Log.i(TAG, "onIceConnectionChange: " + b);
    }

    @Override
    public void onIceGatheringChange(PeerConnection.IceGatheringState iceGatheringState) {
        Log.i(TAG, "onIceGatheringChange: " + iceGatheringState);
    }

    @Override
    public void onIceCandidate(IceCandidate iceCandidate) {
        Log.i(TAG, "onIceCandidate: " + iceCandidate);

        try {
            JSONObject message = new JSONObject();
            //message.put("userId", RTCSignalClient.getInstance().getUserId());
            message.put("type", "candidate");
            message.put("label", iceCandidate.sdpMLineIndex);
            message.put("id", iceCandidate.sdpMid);
            message.put("candidate", iceCandidate.sdp);
            SocketClient.getInstance().sendSignal(message, socketId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onIceCandidatesRemoved(IceCandidate[] iceCandidates) {
        for (int i = 0; i < iceCandidates.length; i++) {
            Log.i(TAG, "onIceCandidatesRemoved: " + iceCandidates[i]);
        }
        PeerConnection peerConnection = SocketClient.getInstance().peers.get(socketId);
        if(peerConnection != null)
            peerConnection.removeIceCandidates(iceCandidates);
    }

    @Override
    public void onAddStream(MediaStream mediaStream) {
        Log.i(TAG, "onAddStream: " + mediaStream.videoTracks.size());
    }

    @Override
    public void onRemoveStream(MediaStream mediaStream) {
        Log.i(TAG, "onRemoveStream");
    }

    @Override
    public void onDataChannel(DataChannel dataChannel) {
        Log.i(TAG, "onDataChannel");
        if(!initial) {
            dataChannel.registerObserver(new SimpleChannelObserver());
            SocketClient.getInstance().channels.put(socketId, dataChannel);
        }
    }

    @Override
    public void onRenegotiationNeeded() {
        Log.i(TAG, "onRenegotiationNeeded");
    }

    @Override
    public void onAddTrack(RtpReceiver rtpReceiver, MediaStream[] mediaStreams) {
        System.out.println("=====================onAddVideoTrack==========================");
        MediaStreamTrack track = rtpReceiver.track();
        if (track instanceof VideoTrack) {
            Log.i(TAG, "onAddVideoTrack");
            VideoTrack remoteVideoTrack = (VideoTrack) track;
            remoteVideoTrack.setEnabled(true);
            for (SurfaceViewRenderer surfaceView : SocketClient.getInstance().surfaceViews) {
                if (surfaceView.getTag() == null) {
                    surfaceView.setTag(socketId);
                    remoteVideoTrack.addSink(surfaceView);
                    break;
                }
            }
        }
    }

    @Override
    public void onTrack(RtpTransceiver transceiver) {
        System.out.println("==================onTrack===============:" + transceiver);
    }
}
